package com.wwinsoft.oa.web.dept;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.wwinsoft.framework.entity.security.Authority;
import com.wwinsoft.framework.entity.security.Group;
import com.wwinsoft.framework.pojo.common.JsNode;
import com.wwinsoft.framework.pojo.common.MenuTree;
import com.wwinsoft.framework.service.security.AccountManager;
import com.wwinsoft.framework.utils.JsNodeHelper;
import com.wwinsoft.framework.utils.MenuUtils;
import com.wwinsoft.framework.web.CrudActionSupport;
import com.wwinsoft.modules.utils.encode.JsonBinder;
import com.wwinsoft.modules.utils.reflection.ConvertUtils;
import com.wwinsoft.modules.utils.web.struts2.Struts2Utils;

@Namespace("/dept")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "dept.action", type = "redirect") })
public class DeptAction extends CrudActionSupport<Group> {

    private static final long serialVersionUID = -4052047494894591406L;

    private AccountManager accountManager;

    private JsonBinder jsonBinder = JsonBinder.buildNormalBinder();

    //-- 页面属性 --//
    private Long id;
    // add by sevenSpend 2011-11-27 start -->
    private String deptId;

    private String name;
    // <-- add by sevenSpend 2011-11-27 end
    private Group entity;

    private List<Group> allDeptList;//部门列表

    private List<Long> checkedAuthIds;//页面中钩选的权限id列表
 // add by sevenSpend 2011/11/24 -->
    private String checkedAuthIdsStr;//页面中勾选的权限id拼接出来的字符串  
 // add by sevenSpend 2011/11/24 <--

    //-- ModelDriven 与 Preparable函数 --//
    public Group getModel() {
        return entity;
    }

    public void setId(Long id) {
        this.id = id;
    }

    // add by sevenSpend 2011-11-27 start -->
	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}
    
    public void setName(String name) {
        this.name = name;
    }
    // <-- add by sevenSpend 2011-11-27 end
    
	@Override
    protected void prepareModel() throws Exception {
        if (id!=null) {
            entity = accountManager.getGroupWithAuthorityFilter(id);
        } else {
            entity = new Group();
        }
    }

    //-- CRUD Action 函数 --//
    @Override
    public String list() throws Exception {
        allDeptList = accountManager.getDeptList();
        return SUCCESS;
    }

    @Override
    public String input() throws Exception {    	
        checkedAuthIds = entity.getAuthIds();
        checkedAuthIdsStr = StringUtils.join(checkedAuthIds, ",") ;
        return INPUT;
    }

    @Override
    public String save() throws Exception {
        checkedAuthIds = ConvertUtils.convertSeparatorStringToList(checkedAuthIdsStr, ",", Long.class);
        //保存部门并放入成功信息.
        // update by sevenSpend 2011-11-27 start -->
        entity.popDeptBaseInfo();
        accountManager.saveGroupInfo(entity, checkedAuthIds);
        // <-- update by sevenSpend 2011-11-27 end
        
        addActionMessage("保存部门成功");
        return RELOAD;
    }

    @Override
    public String delete() throws Exception {
        // update by sevenSpend 2011-11-26 start -->
//		accountManager.deleteRole(id);
//        addActionMessage("删除部门成功");        
        
    	// 根据部门ID获取下级部门列表
    	List<Group> lstDept = accountManager.getDeptByParentId(id);
    	if (lstDept != null && lstDept.size() > 0) {
    		addErrorMessage("部门[" + name + "]存在下级部门，不能被删除！");
            return RELOAD;
    	} 

        // add by sevenSpend 2011-11-30 start -->
		List<Object[]> lstMemberShip = accountManager.getMemberShipListByGroupId(deptId);
		if (lstMemberShip != null && lstMemberShip.size() > 0) {
    		addErrorMessage("部门[" + name + "]下存在用户，不能被删除！");
    		return RELOAD;
		}
        // <-- add by sevenSpend 2011-11-30 end		
    		
		accountManager.deleteGroupInfo(id, deptId);
        addActionMessage("删除部门成功");
        // <-- update by sevenSpend 2011-11-26 end
    	
        return RELOAD;
    }

    public void updateDept() throws Exception {
        JsNode<Long>  jsNode = JsNodeHelper.prepareJsNode(Long.class);
        Long returnId = accountManager.updateNode(jsNode);
        StringBuilder returnJson = new StringBuilder();
        returnJson.append("{\"status\":1,\"id\":").append(returnId).append("}");
        Struts2Utils.renderJson(returnJson.toString());
    }

// add by sevenSpend 2011/11/24 -->
    public String getAuthorJson(){
        List<Authority> allAuthor = accountManager.getAllAuthority();
        MenuTree authorTree = MenuUtils.buildMenuTreeWithRoot(allAuthor, "所有权限");
        return jsonBinder.toJson(authorTree);
    }
 // add by sevenSpend 2011/11/24 <--


    public String getDeptJson() {
        List<Group> allDept = accountManager.getAllDept();
        MenuTree menuTree = MenuUtils.buildMenuTreeWithRoot(allDept, "所有部门");
        return jsonBinder.toJson(menuTree);
    }

    //-- 页面属性访问函数 --//

    /**
     * list页面显示所有部门列表.
     */
    public List<Group> getAllDeptList() {
        return allDeptList;
    }

    // update by sevenSpend 2011-11-26 start -->
    public List<Group> getParentDeptList(){
//       return accountManager.getAllDept();    	
       return accountManager.getDeptByCategory();
    }

    /**
     * input页面显示所有授权列表.
     */
    public List<Authority> getAllAuthorityList() {
        return accountManager.getAllAuthority();
    }

    /**
     * input页面显示部门拥有的授权.
     */
    public List<Long> getCheckedAuthIds() {
        return checkedAuthIds;
    }

    /**
     * input页面提交部门拥有的授权.
     */
    public void setCheckedAuthIds(List<Long> checkedAuthIds) {
        this.checkedAuthIds = checkedAuthIds;
    }

    @Autowired
    public void setAccountManager(AccountManager accountManager) {
        this.accountManager = accountManager;
    }


    // add by sevenSpend 2011/11/24 -->
       public String getCheckedAuthIdsStr() {
   		return checkedAuthIdsStr;
   	}

   	public void setCheckedAuthIdsStr(String checkedAuthIdsStr) {
   		this.checkedAuthIdsStr = checkedAuthIdsStr;
   	}

}